package com.jerome.fs.http.server;

/**
 * Core HTTP processing thread for HttpServer.
 *
 * This class implements the Runnable interface and is used to handle an HTTP request.
 * When executed in a thread, this task will handle an HTTP request on a Socket connection
 * and send a response message.
 *
 * @author Jerome
 * @date 2023/8/5
 */

import java.net.Socket;

/**
 * The HttpTask class implements the Runnable interface and is used to handle an HTTP request.
 * When executed in a thread, this task will handle an HTTP request on a Socket connection
 * and send a response message.
 */
public class HttpTask implements Runnable {
    // Socket used for handling the HTTP request
    private Socket socket;
    // HTTP logic handler
    private HttpHandler httpHandler;

    /**
     * Constructs a new HttpTask to handle the specified Socket connection.
     *
     * @param socket      The Socket used for handling the HTTP request.
     * @param httpHandler The HTTP handler responsible for processing the request.
     */
    public HttpTask(Socket socket, HttpHandler httpHandler) {
        this.socket = socket;
        this.httpHandler = httpHandler;
    }

    /**
     * Implements the run method of the Runnable interface, used to handle the HTTP request and send a response message.
     */
    @Override
    public void run() {
        // Check if the socket is null, and throw an exception if it is
        if (socket == null) {
            throw new IllegalArgumentException("Socket cannot be null.");
        }
        // Call the HTTP handler's handle method to process the HTTP request
        httpHandler.handle(socket);
    }
}
